package com.joplus.housekeeper.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.TintTypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.Address;
import com.joplus.housekeeper.utils.StringUtils;

/**
 * author： ccm
 * time： 2017/3/23.
 * description：处理地址用..
 */

public class EditAddressView extends LinearLayout {

    private final LayoutInflater from;
    private RelativeLayout rl_unaddress;
    private LinearLayout ll_address;
    private TextView tv_needname;
    private TextView tv_needphone;
    private TextView tv_needaddress;
    private TextView tv;
    private AddressBean addressBean;
    private View view;
    private int color;
    private LinearLayout ll_back;
    private ImageView iv_arror;

    public EditAddressView(Context context) {
        super(context);
        from = LayoutInflater.from(context);
        initView();
        initEvent();
    }

    public EditAddressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        from = LayoutInflater.from(context);
        initView();
        initEvent();
    }

    public EditAddressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TintTypedArray tintTypedArray = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.TimePick, defStyleAttr, 0);
        color = tintTypedArray.getColor(0, R.color.jo_text_blue);
        from = LayoutInflater.from(context);
        initView();
        initEvent();

    }

    private boolean noClick = true;

    public void noClick() {
        noClick = false;
    }

    private void initView() {
        view = from.inflate(R.layout.view_editaddress, null);
        // view.setBackgroundColor(getResources().getColor(color));
        addView(view);
        rl_unaddress = (RelativeLayout) view.findViewById(R.id.rl_unaddress);
        ll_address = (LinearLayout) view.findViewById(R.id.ll_address);
        tv_needname = (TextView) view.findViewById(R.id.tv_needname);
        tv = (TextView) view.findViewById(R.id.tv);
        tv_needphone = (TextView) view.findViewById(R.id.tv_needphone);
        tv_needaddress = (TextView) view.findViewById(R.id.tv_needaddress);
        iv_arror = (ImageView) view.findViewById(R.id.iv_arror);
        ll_back = (LinearLayout) view.findViewById(R.id.ll);
        initEvent();

    }

    public void setBack() {
        ll_back.setBackgroundColor(getResources().getColor(R.color.white));
        tv.setTextColor(getResources().getColor(R.color.text_03));
        tv_needname.setTextColor(getResources().getColor(R.color.text_03));
        tv_needphone.setTextColor(getResources().getColor(R.color.text_03));
        tv_needaddress.setTextColor(getResources().getColor(R.color.text_03));
    }

    public void noArror() {
        iv_arror.setVisibility(GONE);
    }

    private void initEvent() {
        rl_unaddress.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noClick) {
                    if (goCheck != null) {
                        goCheck.check();
                    }
                }
            }
        });
        ll_address.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noClick) {
                    if (goCheck != null) {
                        goCheck.check();
                    }
                }

            }
        });
    }


    private GoCheck goCheck;

    public void goCheckAddress(GoCheck goCheck) {
        this.goCheck = goCheck;
    }

    public void setAddress(Address add) {
        if (add == null) {
            return;
        }
        rl_unaddress.setVisibility(GONE);
        ll_address.setVisibility(VISIBLE);
        addressBean = new AddressBean();
        addressBean.setAddress(add.getProvince() + add.getCity() + add.getArea() + add.getAddress());

        addressBean.setName(add.getUserName());
        addressBean.setPhone(add.getPhone());
        addressBean.setAddressId(add.getAddressId());

        StringUtils.setText(tv_needaddress, add.getProvince() + add.getCity() + add.getArea() + add.getAddress());
        StringUtils.setText(tv_needname, add.getUserName());
        StringUtils.setText(tv_needphone, add.getPhone());
    }

    public void setAddress(String name, String phone, String address) {

        rl_unaddress.setVisibility(GONE);
        ll_address.setVisibility(VISIBLE);
        addressBean = new AddressBean();
        addressBean.setAddress(address);
        addressBean.setName(name);
        addressBean.setPhone(phone);

        StringUtils.setText(tv_needaddress, address);
        StringUtils.setText(tv_needname, name);
        StringUtils.setText(tv_needphone, phone);
    }

    public AddressBean getAddress() {
        if (addressBean != null) {
            if (addressBean.getName() == null) {
                return null;
            }
            if (addressBean.getPhone() == null) {
                return null;
            }
            if (addressBean.getAddress() == null) {
                return null;
            }
            return addressBean;
        }
        return null;
    }

    public static class AddressBean {
        private String name;
        private String phone;
        private String address;
        private String addressId = "";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getAddressId() {
            return addressId;
        }

        public void setAddressId(String addressId) {
            this.addressId = addressId;
        }
    }

    public interface GoCheck {
        void check();
    }
}

